package com.sevlet;

import com.Bean.*;
import com.dao.ClientDao;
import com.dao.ClientRoomDao;
import com.dao.impl.ClientDaoimpl;
import com.dao.impl.ClientRoomDaoimpl;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.util.JsonUtil;
import com.util.ProxyUtil;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.ConvertUtils;
import org.apache.commons.beanutils.Converter;
import org.apache.commons.beanutils.converters.DateConverter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@WebServlet("/ClientServlet")
public class ClientServlet extends BaseSevlet {

//    public void search(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//
//        ClientDao clientDao = (ClientDao) ProxyUtil.getDaoProxy(new ClientDaoimpl());
////        ClientDaoimpl clientDaoimpl = new ClientDaoimpl();
////        Clientquery clientquery = new Clientquery();
//        JsonUtil jsonUtil = new JsonUtil();
//        String pid = req.getParameter("pid");
//        System.out.println("xxxxx"+pid);
////        clientquery.setPid(pid);
//        try {
////            List<Clientquery> clientqueryList = (List<Clientquery>) clientDao.queryDatabyPid(pid);
//            jsonUtil.setMsg("成功");
////            jsonUtil.setData(clientqueryList);
//
//        } catch (Exception e) {
//            e.printStackTrace();
//            jsonUtil.setCode(1);
//            jsonUtil.setMsg("系统错误:" + e.getMessage());
//        }
//        resp.setContentType("text/html;charset=utf-8");
//        GsonBuilder gsonBuilder = new GsonBuilder();
//        Gson gson = gsonBuilder.create();
//        String jsonStr = gson.toJson(jsonUtil);
//
//        System.out.println("search转换后的字符串为" + jsonStr);
//        PrintWriter out = resp.getWriter();
//        out.print(jsonStr);//把json字符串输出给客户端
//        }

    /**
     * 添加客户
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    public void add(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        ClientDao clientDao = (ClientDao) ProxyUtil.getDaoProxy(new ClientDaoimpl());
        JsonUtil jsonUtil = new JsonUtil() ;
        GsonBuilder gsonBuilder = new GsonBuilder();
        gsonBuilder.setDateFormat("yyyy-MM-dd");
        try {
            Client client = new Client();
            String pname = req.getParameter("pname");
            String pphone = req.getParameter("pphone") ;
            String cli_pid =req.getParameter("ptn");
            String gender =req.getParameter("pgender");
            String birthday =req.getParameter("pbirthday");
            String pid = req.getParameter("pid");
            String substring = (System.currentTimeMillis() + "").substring(5);
            System.out.println("substring  "+substring);

            System.out.println("-------------------------");
            client.setPphone(pphone);
            client.setPbrithday(birthday);
            client.setCliPid(cli_pid);
            client.setPid(pid);
            client.setPno(substring);
            client.setPgender(gender);
            client.setPname(pname);
            int clientList = clientDao.insert(client);

            System.out.println("-------------"+client.toString());
            jsonUtil.setCode(0);
            jsonUtil.setMsg("成功");

        } catch (Exception e) {
            e.printStackTrace();
            jsonUtil.setCode(1);
            jsonUtil.setMsg("系统错误:" + e.getMessage());
        }
        resp.setContentType("text/html;charset=utf-8");

        Gson gson = gsonBuilder.create();
        String jsonStr = gson.toJson(jsonUtil);
        System.out.println("转换后的字符串为" + jsonStr);
        PrintWriter out = resp.getWriter();
        out.print(jsonStr);//把json字符串输出给客户端
    }
    /**
     * 查单
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    public void select(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        ClientRoomDao clientRoomDao = (ClientRoomDao)ProxyUtil.getDaoProxy(new ClientRoomDaoimpl()) ;

        Clientroom clientroom= new Clientroom();
        GsonBuilder gsonBuilder = new GsonBuilder();

        String phone = req.getParameter("phone");
        String name = req.getParameter("name");
        String ptn = req.getParameter("identity");
////        String[] lasttimes = req.getParameterValues("lasttime");
//
//        String[] times =req.getParameterValues("lasttime");
////        System.out.println(phone);
////        if(times!=null){
////            clientroom.setBeginTime(times[0]);
////            clientroom.setEndTime(times[1]);
////        }
//        System.out.println("times"+times[0]);
        clientroom.setPphone(phone);
        clientroom.setPname(name);
        clientroom.setPtn(ptn);

        JsonUtil jsonUtil = new JsonUtil() ;
        try {

            List<Clientroom> clientroomList =  clientRoomDao.queryData(clientroom);
            jsonUtil.setMsg("成功");
            jsonUtil.setData(clientroomList);

        } catch (Exception e) {
            e.printStackTrace();
            jsonUtil.setCode(1);
            jsonUtil.setMsg("系统错误:" + e.getMessage());

        }
        resp.setContentType("text/html;charset=utf-8");

        Gson gson = gsonBuilder.create();
        String jsonStr = gson.toJson(jsonUtil);

        System.out.println("转换后的字符串为" + jsonStr);
        PrintWriter out = resp.getWriter();

        out.print(jsonStr);//把json字符串输出给客户端


    }

    /**
     *  拉黑
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    public void update(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        ClientDao clientDao = (ClientDao) ProxyUtil.getDaoProxy(new ClientDaoimpl());
        JsonUtil jsonUtil = new JsonUtil();
        Client client = new Client();
        String pid = req.getParameter("pid");
        client.setPid(pid);
        try {
            int clientList = clientDao.update(client);

            jsonUtil.setMsg("成功");
            jsonUtil.setData(clientList);

        } catch (Exception e) {
            e.printStackTrace();
            jsonUtil.setCode(1);
            jsonUtil.setMsg("系统错误:" + e.getMessage());
        }
        resp.setContentType("text/html;charset=utf-8");
        GsonBuilder gsonBuilder = new GsonBuilder();
        Gson gson = gsonBuilder.create();
        String jsonStr = gson.toJson(jsonUtil);

        System.out.println("search转换后的字符串为" + jsonStr);
        PrintWriter out = resp.getWriter();
        out.print(jsonStr);//把json字符串输出给客户端
    }


    /**
     *  取消拉黑
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    public void blankupdate(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        ClientDao clientDao = (ClientDao) ProxyUtil.getDaoProxy(new ClientDaoimpl());
        JsonUtil jsonUtil = new JsonUtil();
        Client client = new Client();
        String pid = req.getParameter("pid");
        client.setPid(pid);
        System.out.println("xxxxxxxxx"+pid);
        try {
            int clientList = clientDao.blankUpdate(client);

            jsonUtil.setMsg("成功");
            jsonUtil.setCode(0);
            jsonUtil.setData(clientList);

        } catch (Exception e) {
            e.printStackTrace();
            jsonUtil.setCode(1);
            jsonUtil.setMsg("系统错误:" + e.getMessage());
        }
        resp.setContentType("text/html;charset=utf-8");
        GsonBuilder gsonBuilder = new GsonBuilder();
        Gson gson = gsonBuilder.create();
        String jsonStr = gson.toJson(jsonUtil);

        System.out.println("search转换后的字符串为" + jsonStr);
        PrintWriter out = resp.getWriter();
        out.print(jsonStr);//把json字符串输出给客户端
    }

    /**
     *   详情表
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    public void updateClient(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException, ParseException {

        ClientDao clientDao = (ClientDao) ProxyUtil.getDaoProxy(new ClientDaoimpl());
        JsonUtil jsonUtil = new JsonUtil();
        Client client = new Client();

        try {

            ConvertUtils.register(new Converter() {

                public Object convert(Class type, Object value) {

                    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
                    try {
                        return simpleDateFormat.parse(value.toString());
                    } catch (ParseException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                    return null;
                }
            }, Date.class);
            BeanUtils.populate(client,req.getParameterMap());

            int clientList = clientDao.updateclient(client);
            jsonUtil.setMsg("成功");
            jsonUtil.setCode(0);
            jsonUtil.setData(clientList);
            System.out.println();
        } catch (Exception e) {
            e.printStackTrace();
            jsonUtil.setCode(1);
            jsonUtil.setMsg("系统错误:" + e.getMessage());
        }
        resp.setContentType("text/html;charset=utf-8");
        GsonBuilder gsonBuilder = new GsonBuilder();
        Gson gson = gsonBuilder.create();
        String jsonStr = gson.toJson(jsonUtil);

        System.out.println("updateClient转换后的字符串为" + jsonStr);
        PrintWriter out = resp.getWriter();
        out.print(jsonStr);//把json字符串输出给客户端
    }

}
